import datetime
import json
from datetime import timedelta

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from flight.models import *


def getAll(request):
    # 航班名
    # 持续日期
    # 起飞机场、时间
    # 到达机场、时间
    # price
    # 最大数量、剩余数量
    # 状态
    flights = FlightInfo.objects.all()
    data = {'root': []}
    count = 0
    for flight in flights:
        no = []
        flightInfo = {'flightName': flight.flightName, 'startPos': flight.startPos, 'startTime': str(flight.startTime),
                      'endPos': flight.endPos, 'endTime': str(flight.endTime), 'price': str(flight.price),
                      'maxSeat': flight.maxSeat, 'leftSeat': flight.leftSeat, 'state': flight.state,
                      'startAir': flight.startAir, 'endAir': flight.endAir, 'flightId': flight.id}
        no.append(flightInfo)
        count += 1
        data['root'].append(no)
    return HttpResponse(json.dumps(data))


def insert(request):
    if request.method == "POST":
        req = json.loads(request.body)
        new_flight = FlightInfo()
        new_flight.flightName = req['flightName']
        dura = req['dura']
        new_flight.startPos = req['startPos']
        new_flight.startTime = req['startTime']
        new_flight.endPos = req['endPos']
        new_flight.endTime = req['endTime']
        new_flight.price = req['price']
        new_flight.maxSeat = req['maxSeat']
        new_flight.leftSeat = req['maxSeat']
        new_flight.startAir = req['startAir']
        new_flight.endAir = req['endAir']
        dseatTime = datetime.datetime.strptime(str(new_flight.startTime), "%Y-%m-%d %H:%M:%S")
        dboardingTime = datetime.datetime.strptime(str(new_flight.startTime), "%Y-%m-%d %H:%M:%S")
        delta = timedelta(days=3)
        dseatTime -= delta
        delta2 = timedelta(seconds=1800)
        dboardingTime -= delta2
        new_flight.dseatTime = dseatTime
        new_flight.dboardingTime = dboardingTime
        new_flight.dterminalTime = req['startTime']
        seatt = ""
        for i in range(int(req['maxSeat'])):
            seatt += "1"
        delta = timedelta(days=1)
        i = 2
        for i in range(int(dura)):
            neww_flight = FlightInfo()
            neww_flight.flightName = req['flightName']
            neww_flight.startPos = req['startPos']
            neww_flight.endPos = req['endPos']
            neww_flight.price = req['price']
            neww_flight.maxSeat = req['maxSeat']
            neww_flight.leftSeat = req['maxSeat']
            neww_flight.startAir = req['startAir']
            neww_flight.endAir = req['endAir']
            neww_flight.seatInfo = seatt
            a = dd = datetime.datetime.strptime(str(new_flight.startTime), "%Y-%m-%d %H:%M:%S")
            a += delta * i

            neww_flight.startTime = a
            a = dd = datetime.datetime.strptime(str(new_flight.endTime), "%Y-%m-%d %H:%M:%S")
            a += delta * i

            neww_flight.endTime = a
            a = dd = datetime.datetime.strptime(str(new_flight.dboardingTime), "%Y-%m-%d %H:%M:%S")
            a += delta * i

            neww_flight.dboardingTime = a
            a = dd = datetime.datetime.strptime(str(new_flight.dseatTime), "%Y-%m-%d %H:%M:%S")
            a += delta * i

            neww_flight.dseatTime = a
            neww_flight.save()
            i += 1
        data = {
            'success': 1,
        }
        return HttpResponse(json.dumps(data))


def delplane(reqquest):
    if reqquest.method == "POST":
        req = json.loads(reqquest.body)
        id = req['flightId']
        flight = FlightInfo.objects.get(id=id)
        flight.delete()
        data = {
            'success': 1,
        }
        return HttpResponse(json.dumps(data))


def changeState(request):
    if request.method == "POST":
        req = json.loads(request.body)
        Id = req['flightId']
        state = req['state']
        flight = FlightInfo.objects.get(id=Id)
        flight.state = state
        flight.save()
        data = {
            'success': 1,
        }
        return HttpResponse(json.dumps(data))


def changetime(request):
    if request.method == "POST":
        req = json.loads(request.body)
        id = req['flightId']
        starttimme = req['startTime']
        endtime = req['endTime']
        flight = FlightInfo.objects.get(id=id)
        flight.startTime = starttimme
        flight.endTime = endtime
        flight.save()
        data = {
            'success': 1,
        }
        return HttpResponse(json.dumps(data))
